<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header" @click="backChange">
        <el-icon size="20"><ArrowLeft /></el-icon>
        <span> 编辑友情链接</span>
      </div>
    </template>
    <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="链接名称" prop="name">
        <el-input
          v-model="form.name"
          placeholder="请输入"
          style="width: 200px"
        />
      </el-form-item>
      <el-form-item label="链接地址" prop="url">
        <el-input
          v-model="form.url"
          placeholder="请输入"
          style="width: 200px"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          style="width: 170px; height: 40px"
          @click="submitForm"
          >保存</el-button
        ></el-form-item
      >
    </el-form>
  </el-card>
</template>
<script setup>
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { add } from '@/api/friendshipLink/index';
import { ElMessage } from 'element-plus';
const router = useRouter();
const route = useRoute();
const formRef = ref(null);
let row = ref('');
const form = ref({
  name: '1',
  url: '2',
});
const rules = {
  name: [{ required: true, message: '请输入链接名称', trigger: 'blur' }],
  url: [{ required: true, message: '请输入链接地址', trigger: 'blur' }],
};
const submitForm = () => {
  let params = ref({
    id: row.value.id,
    name: form.value.name,
    domain: form.value.url,
    type: 5,
    storeid: JSON.parse(localStorage.getItem('userDateInfo'))?.storeId,
  });
  add(params.value).then((res) => {
    ElMessage({
      message: res.msg,
      type: 'success',
    });
    router.push({ path: '/showManage/friendLink' });
  });
};
const backChange = () => {
  router.go(-1);
};

onMounted(() => {
  row.value = JSON.parse(route.query.row);
  form.value.name = JSON.parse(route.query.row).name;
  form.value.url = JSON.parse(route.query.row).domain;
});
</script>

<style lang="scss" scoped>
.box-card {
  margin: 20px;

  .card-header {
    font-size: 20px;
    font-weight: 600;
    color: #1d2129;
    padding: 10px;
    display: flex;
    align-items: center;
    cursor: pointer;
    .headerSize {
      color: #4e5969;
      font-size: 14px;
      margin-left: 16px;
      font-weight: 400;
      height: 20px;
      line-height: 21px;
    }
  }
}
.tip {
  width: 200px;
}
.mt30 {
  margin-top: 30px;
}
</style>
